package com.tongxin.van.frameworkcanary.wxapi;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.widget.LinearLayout;

import com.blankj.utilcode.util.ToastUtils;
import com.tencent.mm.opensdk.constants.ConstantsAPI;
import com.tencent.mm.opensdk.modelbase.BaseReq;
import com.tencent.mm.opensdk.modelbase.BaseResp;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;
import com.tongxin.van.frameworkcanary.App;
import com.tongxin.van.frameworkcanary.utils.WXUtils;


/**
 *
 * author: VanyLiu
 * email: 13212674795@163.com
 * description:微信确认支付操作类
 *
**/
public class WXPayEntryActivity extends Activity implements IWXAPIEventHandler {

    private static final String TAG = "WXPayEntryActivity";

    private IWXAPI api;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(new LinearLayout(this));

        api = WXAPIFactory.createWXAPI(this, WXUtils.APPID);
        api.registerApp(WXUtils.APPID);
        api.handleIntent(getIntent(), this);
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        setIntent(intent);
        api.handleIntent(intent, this);
    }

    @Override
    public void onReq(BaseReq req) {
    }

    @Override
    public void onResp(BaseResp resp) {
        Log.d(TAG, "onPayFinish, errCode = " + resp.errCode);
        if (resp.getType() == ConstantsAPI.COMMAND_PAY_BY_WX) { //微信支付
            switch (resp.errCode) {
                case 0: //支付成功
                    switch (App.wxPay_from) {

                    }
                    ToastUtils.showShort("支付成功");
                    finish();
                    break;
                case -1: //支付失败
                    switch (App.wxPay_from) {

                    }
                    ToastUtils.showShort("支付失败，请重新支付");
                    finish();
                    break;
                case -2: //支付取消
                    switch (App.wxPay_from) {

                    }
                    ToastUtils.showShort("支付取消");
                    finish();
                    break;
                default:
                    ToastUtils.showShort("支付出错，错误码" + resp.errCode);
                    finish();
                    break;
            }
        }
    }
}